package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.WorkService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/work")
public class WorkController {
    @Autowired
    WorkService workService;

    @RequestMapping("/work-list")
    @ResponseBody
    public Map<String,Object> kehuList(String workType, String workContent, Integer pageNum, Integer pageSize, HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum, pageSize);
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        int empId  = 0;
        System.err.println("IDIDIDIDI:"+us.getEmpId());
        if(jueNameList.indexOf("boss")>-1){
            empId = 0;
        }else{
            empId = us.getEmpId();
        }

        List<Work> workList = workService.allworks(workContent,workType,empId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",workList);
        return map;
    }
    //员工查询
    @RequestMapping("/user-list")
    @ResponseBody
    public List<User> userList(){
        System.out.println("进来了了");
        return workService.users();
    }

    //新增
    @RequestMapping("/add-work")
    @ResponseBody
    public void addWork(@RequestBody Work work){
        workService.addWork(work);
    }

    //修改点评


    //统计图日周月报类型查询
    @RequestMapping("/work-lxchaxu")
    @ResponseBody
    public Map<String,Object> workleixing(String vv,HttpSession session){
        Map<String,Object> map = null;
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        int empId  = 0;
        System.err.println("IDIDIDIDI:"+us.getEmpId());
        if(jueNameList.indexOf("boss")>-1){
            empId = 0;
        }else{
            empId = us.getEmpId();
        }
        if(vv.equals("日周月报类型数量统计")) {
            map = workService.workleixing(empId);
        }else{
            map = workService.kehufuzeren(empId);
        }
        return map;
    }
}
